package com.ice.framework.web;

import com.ice.common.domain.UserJwt;
import com.ice.utils.XcOauth2Util;
import org.springframework.web.bind.annotation.ModelAttribute;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Created by mrt on 2018/5/22.
 * 利用spring web提供的@ModelAttribute注解
 *  注解@ModelAttribute放在类方法的参数前面表示引用Model中的数据
 *  注解@ModelAttribute放在类方法上面则表示该Action类中的每个请求调用之前都会执行该方法
 *  因此在此方法里面可以做一些请求预处理，利用此特性就可以抽象出一个基本Action类，然后所有的Action类都继承自这个基本Aciton类，在基本Action类里面获取相应的request、response、session对象：
 */
public class BaseController {
    protected HttpServletRequest request;

    protected HttpServletResponse response;

    protected HttpSession session;

    @ModelAttribute
    public void setReqAndRes(HttpServletRequest request, HttpServletResponse response) {

        this.request = request;

        this.response = response;

        this.session = request.getSession();

    }

    //当前用户id
    public String getUserId(){
        XcOauth2Util xcOauth2Util = new XcOauth2Util();
        UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
        return userJwt.getId();
    }
    //获取当前用户
    public UserJwt getUser(){
        XcOauth2Util xcOauth2Util = new XcOauth2Util();
        UserJwt userJwt = xcOauth2Util.getUserJwtFromHeader(request);
        return userJwt;
    }

}
